//
//  TT_ShareManager.m
//  BaseProject
//
//  Created by 王洋 on 2020/8/5.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "TT_ShareManager.h"
#import "LSTPopView.h"
#import "BA_ShareActionView.h"

@interface TT_ShareManager ()

@property (nonatomic, strong) LSTPopView *popView;
@property (nonatomic, strong) BA_ShareActionView *shareView;

@end

@implementation TT_ShareManager

+ (TT_ShareManager *)share {
    
    static TT_ShareManager *shareInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        shareInstance = [TT_ShareManager new];
    });
    return shareInstance;
}

- (void)callShareViewWithModel:(CommonShareModel *)model {

    if (![model.url containsString:@"myCode"]) {
        NSString *url = model.url;
        model.url = [NSString stringWithFormat:@"%@&myCode=%@",url,[TTUserModel sharedObject].myCode];
    }
    
    self.model = model;
    [self callShareAction];
    [self initViewAction];
}

- (void)initViewAction {
    
    WEAKSELF
    self.shareView.BA_ShareActionViewBlock = ^(BA_ShareActionType index) {
    
        NSString *shareUrl = weakSelf.model.url;
        NSString *shareTitle = weakSelf.model.title;
        NSString *shareContent = weakSelf.model.content.length > 110?[weakSelf.model.content substringToIndex:100]:weakSelf.model.content;

        switch (index) {
            case BA_ShareAction_wechatFriend:{
                
                [weakSelf shareWith:SSDKPlatformSubTypeWechatSession title:shareTitle text:shareContent images:weakSelf.model.image Andurl:shareUrl];

            } break;
            case BA_ShareAction_wechatCircle:{
                
                [weakSelf shareWith:SSDKPlatformSubTypeWechatTimeline title:shareTitle text:shareContent images:weakSelf.model.image Andurl:shareUrl];
            } break;
            case BA_ShareAction_qqFriend:{
                
                [weakSelf shareWith:SSDKPlatformSubTypeQQFriend title:shareTitle text:shareContent images:weakSelf.model.image Andurl:shareUrl];
            } break;
            case BA_ShareAction_weibo:{
                
                [weakSelf shareWith:SSDKPlatformTypeSinaWeibo title:shareTitle text:shareContent images:weakSelf.model.image Andurl:shareUrl];
            } break;
            case BA_ShareAction_close:{

                //[weakSelf.popView dismiss];

            } break;
            default:
                break;
        }
        
        [weakSelf.popView dismiss];
    };
    
}

- (void)shareWith:(SSDKPlatformType)platformType title:(NSString*)title text:(NSString*)text images:(id)image Andurl:(NSString*)url {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params SSDKSetupShareParamsByText:text
                                images:image
                                   url:[NSURL URLWithString:url]
                                 title:title
                                  type:SSDKContentTypeWebPage];
    
    
    [ShareSDK share:platformType
             parameters:params
    onStateChanged:^(SSDKResponseState state,
    NSDictionary *userData, SSDKContentEntity *contentEntity,
    NSError *error) {

        switch (state) {
              case SSDKResponseStateSuccess:
                        NSLog(@"成功");//成功
                        break;
              case SSDKResponseStateFail: {
                        NSLog(@"--%@",error.description);
                        //失败
                   } break;
              case SSDKResponseStateCancel:
                break;

              default:
              break;
        }
    }];
    
    
}

- (void)shareWith:(SSDKPlatformType)platformType images:(id)image {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params SSDKSetupWeChatParamsByText:@"" title:@"" url:nil thumbImage:nil image:image musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil sourceFileExtension:@"" sourceFileData:nil type:SSDKContentTypeImage forPlatformSubType:platformType];
    
    
    [ShareSDK share:platformType
             parameters:params
    onStateChanged:^(SSDKResponseState state,
    NSDictionary *userData, SSDKContentEntity *contentEntity,
    NSError *error) {

        switch (state) {
              case SSDKResponseStateSuccess:
                        NSLog(@"成功");//成功
                        break;
              case SSDKResponseStateFail: {
                        NSLog(@"--%@",error.description);
                        //失败
                   } break;
              case SSDKResponseStateCancel:
                break;

              default:
              break;
        }
    }];
    
    
}

- (void)callShareAction {
    
    self.shareView.frame = CGRectMake(10, 10, TTScreenWidth,200);
    self.popView = [LSTPopView initWithCustomView:self.shareView
                                                popStyle:LSTPopStyleSmoothFromBottom
                                            dismissStyle:LSTDismissStyleSmoothToBottom];
    self.popView.hemStyle = LSTHemStyleBottom;

    WEAKSELF
    self.popView.popDuration = 0.5;
    self.popView.dismissDuration = 0.5;
    self.popView.isClickFeedback = YES;
    self.popView.bgClickBlock = ^{

        [weakSelf.popView dismiss];
    };

    [self.popView pop];
}


- (BA_ShareActionView *)shareView {
    if (!_shareView) {
        _shareView = [[NSBundle mainBundle] loadNibNamed:@"BA_ShareActionView"owner:nil options:nil].firstObject;
    }
    return _shareView;
}

@end
